﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Navigation;
using RM.WebClient.RMServices;
using RM.WebClient.Utilities;

namespace RM.WebClient
{
    public partial class AddEditQuestion : Page
    {
        #region Declaration
        Question question = null;
        QuestionServiceClient QuestionClient = null;
        #endregion

        public AddEditQuestion()
        {
            InitializeComponent();
            Loaded += new RoutedEventHandler(AddEditQuestion_Loaded);
        }

        void AddEditQuestion_Loaded(object sender, RoutedEventArgs e)
        {
            if (!NavigationContext.QueryString.ContainsKey("id"))
            {
                question = new Question();
                InitializeQuestion(question);
            }
            else
            {
                int id=0;
                int.TryParse(NavigationContext.QueryString["id"],out id);
                QuestionClient = new QuestionServiceClient();
                QuestionClient.Question_GetByIdCompleted += new EventHandler<Question_GetByIdCompletedEventArgs>(QuestionClient_GetByIdCompleted);
                QuestionClient.Question_GetByIdAsync(id);
            }
        }

        private void InitializeQuestion(Question question)
        {
            this.DataContext = question;
            if (question.QuestionList == null)
                question.QuestionList = new System.Collections.ObjectModel.ObservableCollection<QuestionDetails>();
            dataGrid.ItemsSource = question.QuestionList;
        }

        void QuestionClient_GetByIdCompleted(object sender, Question_GetByIdCompletedEventArgs e)
        {
            question = e.Result;
            InitializeQuestion(question);
        }

        // Executes when the user navigates to this page.
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {

        }

        private void Save_Click(object sender, RoutedEventArgs e)
        {
            UpdateFields();
            if (ValidateFields())
            {
                QuestionClient = new QuestionServiceClient();
                question.CreatedDate = DateTime.Now;
                question.ModifiedDate = DateTime.Now;
                QuestionClient.Question_SaveCompleted += new EventHandler<System.ComponentModel.AsyncCompletedEventArgs>(QuestionClient_SaveCompleted);
                QuestionClient.Question_SaveAsync(question);
            }
        }

        private bool ValidateFields()
        {
            bool IsValid = txtName.validateField("Question Name");
            IsValid = txtTechnology.validateField("Technology") && IsValid;
            IsValid = txtLanguage.validateField("Technology") && IsValid;
            IsValid = txtExperience.validateField("Technology") && IsValid;
            IsValid = txtExperience.IsNumberValid()&& IsValid;
            
            return IsValid;
        }

        void QuestionClient_SaveCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e)
        {
            MessageDialog.Show(Constants.MsgQuestionSuccess);
        }

        private void UpdateFields()
        {
            txtExperience.GetBindingExpression(TextBox.TextProperty).UpdateSource();
            txtLanguage.GetBindingExpression(TextBox.TextProperty).UpdateSource();
            txtTechnology.GetBindingExpression(TextBox.TextProperty).UpdateSource();
        }

        private void btnAddQues_Click(object sender, RoutedEventArgs e)
        {
            AddQuestionWindow Createwindow = new AddQuestionWindow();
            Createwindow.OnSaveCompleted += new AddQuestionWindow.SaveCompleted(Createwindow_OnSaveCompleted);
            Createwindow.Show();
        }

        void Createwindow_OnSaveCompleted(string Content)
        {
           QuestionDetails Qdetails= new QuestionDetails();
           Qdetails.Content = Content;
           Qdetails.CreatedDate = DateTime.Now;
           Qdetails.ModifiedDate = DateTime.Now;
           question.QuestionList.Add(Qdetails);
        }

        private void Edit_MouseUp(object sender, MouseButtonEventArgs e)
        {
            QuestionDetails quest = dataGrid.SelectedItem as QuestionDetails;
            AddQuestionWindow Createwindow = new AddQuestionWindow(quest.Content);
            Createwindow.OnSaveCompleted += new AddQuestionWindow.SaveCompleted(Edit_OnSaveCompleted);
            Createwindow.Show();

        }
        void Edit_OnSaveCompleted(string Content)
        {
            QuestionDetails quest= dataGrid.SelectedItem as QuestionDetails;
            quest.Content = Content;
        }
    }
}
